//
//  HLUserDetailCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/29.
//

#import "HLUserDetailCell.h"
#import "HLVideoPlayViewController.h"
#import "HLUserOptionRequest.h"

@interface HLUserDetailCell ()

@property (nonatomic, strong) UILabel *dayLabel;
@property (nonatomic, strong) UILabel *monthLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *coverImageView;
@property (nonatomic, strong) QMUIButton *playButton;
@property (nonatomic, strong) QMUIButton *likeButton;

@end

@implementation HLUserDetailCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.dayLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(26) textColor:HLTextColor1];
    _dayLabel.text = @"08";
    [self.contentView addSubview:_dayLabel];
    [_dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
    }];
    
    self.monthLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(14) textColor:[UIColor qmui_colorWithHexString:@"#595A69"]];
    _monthLabel.text = @"03 月";
    [self.contentView addSubview:_monthLabel];
    [_monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(52);
        make.centerY.equalTo(_dayLabel);
    }];
    
    self.contentLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(14) textColor:HLTextColor1];
    _contentLabel.text = @"没有愚蠢的问题，只有愚蠢的回答";
    _contentLabel.numberOfLines = 0;
    _contentLabel.qmui_lineHeight = 20;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_monthLabel);
        make.top.equalTo(_dayLabel.mas_bottom).offset(10);
        make.right.mas_equalTo(-12);
    }];
    
    self.coverImageView = [[UIImageView alloc] init];
    _coverImageView.layer.masksToBounds = YES;
    _coverImageView.layer.cornerRadius = 8;
    _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_coverImageView];
    [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_contentLabel);
        make.top.equalTo(_contentLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(200);
    }];
    
    self.playButton = [[QMUIButton alloc] init];
    [_playButton setImage:UIImageMake(@"user_detail_cell_play") forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_playButton];
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_coverImageView);
    }];
    
    self.likeButton = [[QMUIButton alloc] init];
    [_likeButton setTitle:@"62" forState:UIControlStateNormal];
    [_likeButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
    [_likeButton setTitleColor:HLTextColor3 forState:UIControlStateSelected];
    [_likeButton setImage:UIImageMake(@"square_cell_like_hl") forState:UIControlStateSelected];
    [_likeButton setImage:UIImageMake(@"square_cell_like") forState:UIControlStateNormal];
    _likeButton.titleLabel.font = UIFontMake(13);
    _likeButton.imagePosition = QMUIButtonImagePositionLeft;
    _likeButton.spacingBetweenImageAndTitle = 8;
    [_likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_likeButton];
    
    [_likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_coverImageView);
        make.top.equalTo(_coverImageView.mas_bottom).offset(12);
        make.bottom.mas_equalTo(-12);
    }];
}

- (void)setModel:(HLUserVideoModel *)model
{
    _model = model;
    
    NSDate *date = [NSDate br_dateFromString:model.uploadTime dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    _contentLabel.text = model.videoDesc;
    [_coverImageView sd_setImageWithURL:[NSURL URLWithString:model.imgLink]];
    [_likeButton setTitle:model.thumbUpCount forState:UIControlStateNormal];
    _likeButton.selected = [model.isThumbUp boolValue];
    NSString *dateString = [NSDate br_stringFromDate:date dateFormat:@"yyyy-MM-dd"];
    NSArray<NSString *> *datas = [dateString componentsSeparatedByString:@"-"];
    _dayLabel.text = [NSString stringWithFormat:@"%@", datas.lastObject];
    _monthLabel.text = [NSString stringWithFormat:@"%@ 月", datas[1]];
    
    
}

- (void)likeAction:(QMUIButton *)btn
{
    if (!btn.isSelected){
        HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeVideoLike params:@{@"shortVideoCode":self.model.shortVideoCode}];
        [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                NSString *thumbCount = [NSString stringWithFormat:@"%ld",([self.model.thumbUpCount integerValue] + 1)];
                [self.likeButton setTitle:thumbCount forState:UIControlStateNormal];
                self.model.thumbUpCount = thumbCount;
                btn.selected = YES;
            }
        } failure:nil];
    } else {
        HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeVideoUnLike params:@{@"shortVideoCode":self.model.shortVideoCode}];
        [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                NSString *thumbCount = [NSString stringWithFormat:@"%ld",([self.model.thumbUpCount integerValue] - 1)];
                [self.likeButton setTitle:thumbCount forState:UIControlStateNormal];
                self.model.thumbUpCount = thumbCount;
                btn.selected = NO;
            }
        } failure:nil];
    }
}

- (void)playAction:(QMUIButton *)btn
{
    HLVideoPlayViewController *videoPlayVC = [[HLVideoPlayViewController alloc] initWithUrl:[NSURL URLWithString:_model.shortVideoLink] coverUrl:_model.imgLink];
    [self.qmui_viewController.navigationController pushViewController:videoPlayVC animated:YES];
}

@end
